package com.hn.controller;

import com.hn.pojo.Emp;
import com.hn.pojo.PageBean;
import com.hn.service.EmpService;
import com.hn.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 员工管理Controller
 */
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
//    @GetMapping
//    public Result selectAll(@RequestParam(defaultValue = "1") Integer page,
//                            @RequestParam(defaultValue = "10") Integer pageSize,
//                            String name, int gender,
//                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime begin,
//                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime end
//                            ){
//        PageBean pageBean=empService.selectAll(name,gender,begin,end,page,pageSize);
//        return Result.success(pageBean);
//    }
    /*
    *分页查询加条件查询
    *
     */
    @GetMapping
    public Result select(@RequestParam(defaultValue = "1" ) Integer page,
                         @RequestParam(defaultValue = "10") Integer pageSize,
                         String name, Short gender,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
                         ){
        PageBean pageBean=empService.select(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }
    /*
    *批量删除
     */
    @DeleteMapping("{ids}")
    public Result delete(@PathVariable Integer[] ids){
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    private Result add(@RequestBody Emp emp){
        empService.add(emp);
        return Result.success();
    }
    @GetMapping("{id}")
    public Result selectById(@PathVariable Integer id){
        Emp data=empService.selectById(id);
        return Result.success(data);
    }
    @PutMapping
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }
}
